<!--
 * @Author: 陈祝文 15821704398@163.com
 * @Date: 2024-02-19 15:02:50
 * @LastEditors: 陈祝文 15821704398@163.com
 * @LastEditTime: 2024-02-20 10:50:22
 * @FilePath: \zhaoshang-Les-web\src\components\Table\columnSetting.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div slot="overlay" class="ant-dropdown-menu s-tool-column ant-dropdown-content">
    <div class="s-tool-column-header s-tool-column-item">
      <a-checkbox :indeterminate="indeterminate" :checked="checkAll" @change="onCheckAllChange">
        列展示
      </a-checkbox>
      <a @click="reset">重置</a>
    </div>
    <a-divider />
    <div class="ant-checkbox-group">
      <div>
        <draggable v-model="columnsSetting" animation="300" @end="emitColumnChange">
          <div class="s-tool-column-item" v-for="item in columnsSetting" :key="item.title">
            <div class="s-tool-column-handle" >
              <a-icon type="more"/>
              <a-icon type="more"/>
            </div>
            <a-checkbox v-model="item.checked" @change="onChange">{{ item.title }}</a-checkbox>
          </div>
        </draggable>
      </div>
    </div>
  </div>
</template>

<script>
  import draggable from 'vuedraggable'

  export default {
    props: {
      columns: {
        type: Array,
        default: () => ([])
      }
    },
    components: {
      draggable
    },
    data() {
      return {
        indeterminate: false,
        checkAll: true,
        columnsSetting: [],
        originColumns: []
      }
    },
    methods: {
      reset() {
        const path = this.$route.path
        this.columnsSetting = JSON.parse(JSON.stringify(this.originColumns))
        window.localStorage.removeItem(path)
        this.indeterminate = false
        this.checkAll = true
        this.emitColumnChange()
      },
      onChange() {
        const path = this.$route.path
        window.localStorage.setItem(path,JSON.stringify(this.columnsSetting))
        const checkedList = this.columnsSetting.filter(value => value.checked)
        this.indeterminate = !!checkedList.length && checkedList.length < this.columnsSetting.length
        this.checkAll = checkedList.length === this.columnsSetting.length
        this.emitColumnChange() 
      },
      onCheckAllChange(e) {
        const val = e.target.checked
        Object.assign(this, {
          indeterminate: false,
          checkAll: val,
          columnsSetting: this.columns.map(value => ({ ...value, checked: val }))
        })
        this.emitColumnChange()
      },
      emitColumnChange() {
        this.$emit('columnChange', this.columnsSetting)
      }
    },
    mounted() {
      const path = this.$route.path
      let str =   window.localStorage.getItem(path)
      console.log(str,'str')
      if(str) {
        this.columnsSetting = JSON.parse(str)
        this.originColumns = JSON.parse(JSON.stringify(this.columnsSetting))
      }else {
        this.columnsSetting = this.columns.map(value => ({ ...value, checked: true }))
        this.originColumns = JSON.parse(JSON.stringify(this.columnsSetting))
        window.localStorage.setItem(path,JSON.stringify(columnsSetting))
      }
      
    }
  }
</script>

<style lang="less" scoped>

</style>
